package com.smartleanx.module.hrm.service.esign.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.esign.ElecCorp;
import com.smartleanx.module.hrm.dal.dataobject.esign.EsignOrgMember;
import com.smartleanx.module.hrm.dal.dataobject.esign.EsignOrgSeal;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.ElecCorpMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignOrgMemberMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignOrgSealMapper;
import com.smartleanx.module.hrm.service.esign.auth.CommonService;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import com.smartleanx.module.hrm.service.examine.util.exception.SignException;
import com.smartleanx.module.hrm.util.esign.constant.MemberRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ElecCorpMapper elecCorpMapper;
    @Autowired
    private EsignOrgMemberMapper esignOrgMemberMapper;
    @Autowired
    private EsignOrgSealMapper esignOrgSealMapper;

    @Override
    public EmployeeDO checkEmployeeIdentify(long employeeId) {
        EmployeeDO employee = employeeMapper.selectByUserId(employeeId);
        if (employee.getEsignRealnameStatus() == 0 || employee.getEsignAuthorizedStatus() == 0) {
            throw new SignException("请先完成个人实名认证/授权");
        }
        return employee;
    }

    @Override
    public ElecCorp checkCorpIdentify(long corpId) {
        ElecCorp elecCorp = elecCorpMapper.selectById(corpId);
        if (elecCorp == null) {
            throw new ResultException("企业不存在");
        }
        if (elecCorp.getEsignRealnameStatus() != 1 || elecCorp.getEsignAuthorizedStatus() != 1) {
            throw new ResultException("请先完成企业实名认证/授权");
        }
        return elecCorp;
    }

    @Override
    public List<EsignOrgMember> getOrgMemberRoles(String psnId, String orgId, MemberRole... roles) {
        List<String> roleList =
                Arrays.stream(roles).filter(Objects::nonNull).map(MemberRole::getCode).collect(Collectors.toList());
        List<EsignOrgMember> psnRoles = esignOrgMemberMapper.selectList(new LambdaQueryWrapper<EsignOrgMember>() {
            {
                eq(EsignOrgMember::getPsnId, psnId);
                eq(EsignOrgMember::getOrgId, orgId);
                eq(roleList.size() == 1, EsignOrgMember::getRole, roleList.get(0));
                in(roleList.size() > 1, EsignOrgMember::getRole, roleList);
            }
        });
        if (psnRoles.isEmpty()) {
            throw new ResultException("无操作权限");
        }
        return psnRoles;
    }

    @Override
    public EsignOrgSeal checkOrgSealExist(String orgId, String sealId) {
        EsignOrgSeal orgSeal = esignOrgSealMapper.selectOne(new LambdaQueryWrapper<EsignOrgSeal>() {
            {
                eq(EsignOrgSeal::getOrgId, orgId);
                eq(EsignOrgSeal::getSealId, sealId);
            }
        });
        if (orgSeal == null) {
            throw new ResultException("企业印章不存在，请刷新重试");
        }
        return orgSeal;
    }



}
